package com.orphan.supplier.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.orphan.supplier.mapper.BackgoodsMapper;
import com.orphan.supplier.po.Back;
import com.orphan.supplier.mapper.BackMapper;
import com.orphan.supplier.service.IBackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orphan.supplier.vo.BackVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zjm
 * @since 2021-07-14
 */
@Service
public class BackServiceImpl extends ServiceImpl<BackMapper, Back> implements IBackService {
    @Resource
    BackMapper backMapper;
    @Resource
    IBackService backService;
    @Resource
    BackgoodsMapper backgoodsMapper;
    @Override
    public List<BackVo> findAll() {
        return backMapper.findAll();
    }

    @Override
    public Back findById(String OrderId) {
        return backMapper.findById(OrderId);
    }

    @Override
    public PageInfo<BackVo> findByItem(int pageNO, String state) {
//        List<Back>list=backService.list();
//        List<BackVo> result=new ArrayList<>();
//        for (Back b:
//                list) {
//            result.add(new BackVo(b));
//        }
        PageHelper.startPage(pageNO, PAGESIZE);
        PageInfo<BackVo> pageInfo = PageInfo.of(backMapper.findByItem(state));
        return pageInfo;
    }

    @Override
    public void delBackOrder(String torderId) {
        backMapper.delBackOrder(torderId);
    }

    @Override
    public void addBackOrder(Back back) {
        backMapper.insert(back);
    }

    @Override
    public void updateBackOrderState(String OrderId,Integer goodsNum,String goodsName) {
        backMapper.updateBackOrderState(OrderId);
        //查询该退货商品原有的库存
        Integer number = backgoodsMapper.selectGoodsNum(goodsName);
        //        增加供应商的商品库存
        backgoodsMapper.addGoodsNum(OrderId, number + goodsNum);
    }
}
